package com.example.demo.controller;

import com.example.demo.entity.MusicComment;
import com.example.demo.service.MusicCommentService;
import com.example.demo.vo.ViewMusicCommentInfo;
import com.example.demo.vo.ViewNoteCommentInfo;
import com.example.demo.vo.ViewNoteCreateInfo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("v1")
public class MusicCommentController {
    @Autowired
    private MusicCommentService musicCommentService;

    @GetMapping("/MusicCommentinfo")
    public PageInfo<ViewMusicCommentInfo> getPageMusicCommentInfo(@RequestParam Integer pageNum, @RequestParam Integer pageSize ) {
        PageInfo<ViewMusicCommentInfo> pageInfo = musicCommentService.findMusicCommentInfo(pageNum,pageSize);
        return pageInfo;
    }

    @GetMapping("/pages")
    public PageInfo<MusicComment> getPageMusicComment(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        PageInfo<MusicComment> pageInfo = musicCommentService.findMusicCommentPages(pageNum,pageSize);
        return pageInfo;
    }


    //获取多表关联评论
    @GetMapping("/MusicCommentinfo/{id}")
    public ViewMusicCommentInfo getPageMusicCommentInfoid(@PathVariable Integer id) {
        return musicCommentService.findMusicCommentinfoId(id);
    }


    //得到所有评论
    @GetMapping("/MusicComment")
    public List<MusicComment> getMusicComment() {

        return musicCommentService.findMusicCommentAll();
    }

    //根据id获取评论
    @GetMapping("/MusicComment/{id}")
    public MusicComment getMusicCommentId(@PathVariable Integer id) {
        return musicCommentService.findMusicCommentDyId(id);
    }

    //添加评论
    @PostMapping("/MusicComment")
    public Integer addMusicCommentInfo(@RequestBody MusicComment musicComment) {
        try {
            System.out.println(musicComment);
            return musicCommentService.insertMusicComment(musicComment);
        } catch (Exception e) {
            e.printStackTrace();
            return null; // 或者返回一个错误码
        }
    }

    //修改评论
    @PutMapping("/MusicComment")
    public Integer updateMusicCommentInfo(@RequestBody MusicComment musicComment) {
        System.out.println(musicComment);
        return musicCommentService.updateMusicComment(musicComment);
    }

    //根据评论id删除歌曲评论
    @DeleteMapping("/MusicComment/{id}")
    public Integer deleteMusicComment(@PathVariable("id") Integer music_comment_id) {
        return musicCommentService.deleteMusicComment(music_comment_id);
    }







    //与用户表的用户名、头像相关联，且根据歌曲id查找，按点赞量排序
    @GetMapping("/MusicCommentinfonoteid")
    public PageInfo<ViewMusicCommentInfo> getPageMusicCommentInfomusicid(@RequestParam Integer music_id) {
        PageInfo<ViewMusicCommentInfo> pageInfo = musicCommentService.findMusicCommentInfomusicid(music_id);
        return pageInfo;
    }

    //点击举报按钮将会把评论状态从正常修改为被举报
    @PutMapping("/MusicCommentStatus/{music_comment_id}")
    public Integer updateStatus(@PathVariable Integer music_comment_id) {

        return musicCommentService.updateStatus(music_comment_id);
    }


    //根据笔记id计数
    @GetMapping("/MusicComment/count")
    public ResponseEntity<Integer> countcomment(
            @RequestParam Integer music_id) {
        Integer count = musicCommentService.countcomment(music_id);
        return ResponseEntity.ok(count);
    }

}
